package com.github.jptx1234.MinecraftShiftHelper;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;

import com.tulskiy.keymaster.common.HotKey;
import com.tulskiy.keymaster.common.HotKeyListener;
import com.tulskiy.keymaster.common.Provider;

public class MinecraftShiftHelper {

    final int START_KEY = 0;
    final int STOP_KEY = 1;
    final int SHIFT_KEY = 2;
    JLabel stateMes = new JLabel("已停止，按CTRL+F2开始");
    Robot robot;
    Boolean shiftRunFlag = false;

    public static void main(String[] args) {
        new MinecraftShiftHelper();
    }

    public MinecraftShiftHelper() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception lookAndFeelException) {

        }
        final JFrame w = new JFrame("Minecraft 潜行辅助     --By 剑平天下1234");
        w.setBounds(new Rectangle(290, 110));

        stateMes.setFont(new Font("微软雅黑", Font.BOLD, 40));
        stateMes.setForeground(Color.RED);
        w.add(stateMes);

        w.pack();
        w.setLocationRelativeTo(null);

        w.setVisible(true);
        w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try {
            robot = new Robot();
        } catch (AWTException e) {
            stateMes.setText("robot异常！");
            stateMes.setForeground(Color.RED);
            return;
        }
        final Provider provider = Provider.getCurrentProvider(true);
        provider.register(KeyStroke.getKeyStroke("ctrl F2"), new HotKeyListener() {

            @Override
            public void onHotKey(HotKey hotKey) {
                sendShiftKey(true);
            }
        });
        provider.register(KeyStroke.getKeyStroke("shift ctrl F2"), new HotKeyListener() {

            @Override
            public void onHotKey(HotKey hotKey) {
                sendShiftKey(false);
            }
        });

        w.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                provider.reset();
                provider.stop();
                System.exit(0);
            }
        });
        /**
         JIntellitype.getInstance().registerHotKey(START_KEY, "CTRL+f2");
         JIntellitype.getInstance().registerHotKey(STOP_KEY, "SHIFT+CTRL+f2");
         JIntellitype.getInstance().registerHotKey(SHIFT_KEY, "SHIFT");

         JIntellitype.getInstance().addHotKeyListener(new HotkeyListener() {

        @Override public void onHotKey(int identifier) {
        switch (identifier) {
        case 0:
        break;
        case 1:
        sendShiftKey(false);
        break;
        case 2:
        if (runFlag) {
        sendShiftKey(true);
        }
        break;

        default:
        break;
        }

        }
        });
         */

        //界面部分
        /**
         JPanel content = new JPanel(new GridLayout(2, 1));

         JPanel hotKeyJPanel = new JPanel();
         JLabel hotKeyLabel = new JLabel("启动/停止热键：");
         JTextField hotKeyField = new JTextField("   F10   ");
         JButton hotKeyJButton = new JButton("应用");
         hotKeyJPanel.add(hotKeyLabel);
         hotKeyJPanel.add(hotKeyField);
         hotKeyJPanel.add(hotKeyJButton);

         JPanel shiftKeyJPanel = new JPanel();
         JLabel shiftKeyJLabel = new JLabel(" 修改潜行热键：");
         JTextField shiftKeyField = new JTextField("  Shift  ");
         JButton shiftKeyButton = new JButton("应用");
         shiftKeyJPanel.add(shiftKeyJLabel);
         shiftKeyJPanel.add(shiftKeyField);
         shiftKeyJPanel.add(shiftKeyButton);

         content.add(hotKeyJPanel);
         content.add(shiftKeyJPanel);
         w.add(content);

         hotKeyField.addKeyListener(new KeyListener() {

        @Override public void keyTyped(KeyEvent e) {
        e.consume();
        }

        @Override public void keyReleased(KeyEvent e) {
        e.consume();

        }

        @Override public void keyPressed(KeyEvent e) {
        e.consume();
        int keycode = e.getKeyCode();
        String presskey = "";
        int mod = e.getModifiersEx();
        if ((mod & InputEvent.SHIFT_DOWN_MASK) != 0){
        presskey += "SHIFT ";
        }
        if ((mod & InputEvent.ALT_DOWN_MASK) != 0){
        presskey += "ALT ";
        }
        if ((mod & InputEvent.CTRL_DOWN_MASK) != 0){
        presskey += "CTRL ";
        }
        presskey += KeyEvent.getKeyText(keycode);
        hotKeyField.setText(presskey);

        }
        });
         */
    }

    public void sendShiftKey(boolean isPress) {
        if (isPress) {
            shiftRunFlag = true;
            new Thread(new Runnable() {

                @Override
                public void run() {
                    while (shiftRunFlag) {
                        robot.keyPress(KeyEvent.VK_SHIFT);
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                        }
                    }
                }
            }).start();
            stateMes.setText("已运行，按CTRL+F2停止");
            stateMes.setForeground(Color.GREEN);
        } else {
            shiftRunFlag = false;
            robot.keyRelease(KeyEvent.VK_SHIFT);
            stateMes.setText("已停止，按CTRL+F2运行");
            stateMes.setForeground(Color.RED);
        }
    }

}